package com.fs.base.excel.view;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

/**
 * 2007版Excel视图
 * <p>Created by Raofeicheng on 2017/12/6.</p>
 * @author Raofeicheng
 * @version 1.0
 */
public class XlsxStreamingView extends XlsView {

	/**
	 * 设置ContentType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
	 */
	public XlsxStreamingView() {
		setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
	}

	public XlsxStreamingView(Collection<Object> data) {
		this(data.iterator());
	}

	public XlsxStreamingView(Iterator<Object> data) {
		this();
		setData(data);
	}

	/**
	 * 创建Workbook，使用{@link SXSSFWorkbook}
	 */
	protected Workbook createWorkbook(Map<String, Object> model, HttpServletRequest request) {
		return new SXSSFWorkbook();
	}

	/**
	 * 输出excel，同时释放{@link SXSSFWorkbook}
	 */
	protected void renderWorkbook(Workbook workbook, HttpServletResponse response) throws IOException {
		super.renderWorkbook(workbook, response);
		((SXSSFWorkbook) workbook).dispose();
	}

}
